1
|
|
|
import React from "react" |
2
|
|
|
import expectRender from "../expect-to-same-render" |
3
|
|
|
import type { ClassNames, ClassNamesProperty, ClassValue } from "./defs" |
4
|
|
|
import classNamingBasic from "./basic" |
5
|
|
|
import classNamesCheck from "./check" |
6
|
|
|
|
7
|
|
|
function Button({className, "classnames": { Btn }}: ClassNames<true, ClassNamesProperty<{Btn: ClassValue}>>) { |
8
|
|
|
return <button {...classNamingBasic(className, { Btn })}/> |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
function Root({ |
12
|
|
|
classnames, "classnames": { App__Item, App__Footer } |
13
|
|
|
}: ClassNames<ClassNamesProperty<{App__Item: ClassValue; App__Footer: ClassValue}>, typeof Button>) { |
14
|
|
|
return <> |
15
|
|
|
<Button {...{ |
16
|
|
|
...classNamingBasic({ App__Item }), |
17
|
|
|
classnames |
18
|
|
|
}}/> |
19
|
|
|
<div |
20
|
|
|
className={`${classNamingBasic({App__Footer})}`} |
21
|
|
|
data-classname={`${classNamingBasic({App__Footer})}`} |
22
|
|
|
/> |
23
|
|
|
</> |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
it("not css module", () => expectRender( |
27
|
|
|
<Root classnames={classNamesCheck()}/> |
28
|
|
|
).toSame( |
29
|
|
|
<button className="App__Item Btn"></button>, |
30
|
|
|
<div className="App__Footer" data-classname="App__Footer"></div> |
31
|
|
|
)) |
32
|
|
|
|
33
|
|
|
it("css module", () => expectRender( |
34
|
|
|
<Root classnames={{ |
35
|
|
|
App__Footer: "footer-hash", |
36
|
|
|
App__Item: "item-hash", |
37
|
|
|
Btn: "btn-hash" |
38
|
|
|
}}/> |
39
|
|
|
).toSame( |
40
|
|
|
<button className="item-hash btn-hash"></button>, |
41
|
|
|
<div className="footer-hash" data-classname="footer-hash"></div> |
42
|
|
|
)) |
43
|
|
|
|
44
|
|
|
it("vscode couldn't rename enum element", () => { |
45
|
|
|
function Root({ |
46
|
|
|
"classnames": {App: App__Container} |
47
|
|
|
}: ClassNames<ClassNamesProperty<{App: ClassValue}>>) { |
48
|
|
|
return <div {...classNamingBasic({App: App__Container})}/> |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
expectRender( |
52
|
|
|
<Root classnames={classNamesCheck()} /> |
53
|
|
|
).toSame( |
54
|
|
|
<div className="App"></div> |
55
|
|
|
) |
56
|
|
|
}) |
57
|
|
|
|
58
|
|
|
it("vscode can rename property", () => { |
59
|
|
|
type RootProps = { |
60
|
|
|
classnames: { |
61
|
|
|
App__Container: ClassValue |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function Root({ |
66
|
|
|
"classnames": {App__Container: App__Container} |
67
|
|
|
}: RootProps) { |
68
|
|
|
return <div {...classNamingBasic({App: App__Container})}/> |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function Root2({ |
72
|
|
|
"classnames": { |
73
|
|
|
// VSCode didn't rename here due to `ClassNames<>` wrapper |
74
|
|
|
//@ts-expect-error Property 'App' does not exist |
75
|
|
|
App: App__Container |
76
|
|
|
} |
77
|
|
|
}: ClassNames<typeof Root>) { |
78
|
|
|
return <div {...classNamingBasic({App: App__Container})}/> |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
expectRender( |
82
|
|
|
<Root |
83
|
|
|
classnames={ |
84
|
|
|
classNamesCheck()} />, |
85
|
|
|
<Root2 classnames={classNamesCheck()} /> |
86
|
|
|
).toSame( |
87
|
|
|
<div className="App"/>, |
88
|
|
|
<div className="App"/> |
89
|
|
|
) |
90
|
|
|
}) |
91
|
|
|
|
92
|
|
|
it("additional type check after rename", () => { |
93
|
|
|
type Props1 = { |
94
|
|
|
classnames: { class1: ClassValue } |
95
|
|
|
} |
96
|
|
|
type Props2 = { |
97
|
|
|
classnames: { class2_renamed: ClassValue } |
98
|
|
|
} |
99
|
|
|
const { class1, |
100
|
|
|
//@ts-expect-error Property 'class2' does not exist |
101
|
|
|
class2 |
102
|
|
|
} = classNamesCheck<Props1 & Props2>(); |
103
|
|
|
|
104
|
|
|
expectRender( |
105
|
|
|
<div {...classNamingBasic<Props1>({class1})} />, |
106
|
|
|
<div {...classNamingBasic<Props2>({ |
107
|
|
|
//@ts-expect-error Object literal may only specify known properties, and 'class2' does not exist |
108
|
|
|
class2 |
109
|
|
|
})} /> |
110
|
|
|
).toSame( |
111
|
|
|
<div className="class1"/>, |
112
|
|
|
<div className="class2"/> |
113
|
|
|
) |
114
|
|
|
}) |
115
|
|
|
|
116
|
|
|
it("chaining", () => { |
117
|
|
|
const props = {className: "Cell", classnames: classNamesCheck()} |
118
|
|
|
|
119
|
|
|
const {className, classnames: { |
120
|
|
|
Column_1, Column_2, |
121
|
|
|
Row_1, Row_2 |
122
|
|
|
}} = props |
123
|
|
|
, cn1 = classNamingBasic(className, {Column_1}) |
124
|
|
|
, cn2 = classNamingBasic(className, {Column_2}) |
125
|
|
|
|
126
|
|
|
expectRender( |
127
|
|
|
<div {...cn1({ Row_1 })} />, |
128
|
|
|
<div {...cn1({ Row_2 })} />, |
129
|
|
|
<div {...cn2({ Row_1 })} />, |
130
|
|
|
<div {...cn2({ Row_2 })} />, |
131
|
|
|
<div {...classNamingBasic({ Column_1 })({ Column_2 })({ Row_1 })({ Row_2 })} /> |
132
|
|
|
).toSame( |
133
|
|
|
<div className="Cell Column_1 Row_1" />, |
134
|
|
|
<div className="Cell Column_1 Row_2" />, |
135
|
|
|
<div className="Cell Column_2 Row_1" />, |
136
|
|
|
<div className="Cell Column_2 Row_2" />, |
137
|
|
|
<div className="Column_1 Column_2 Row_1 Row_2" /> |
138
|
|
|
) |
139
|
|
|
}) |
140
|
|
|
|